package com.shicijiaoliu.controller;


import com.shicijiaoliu.pojo.Author;
import com.shicijiaoliu.pojo.Shicitabel;
import com.shicijiaoliu.pojo.User;
import com.shicijiaoliu.pojo.result;
import com.shicijiaoliu.service.Authorservice;
import com.shicijiaoliu.service.Shiciservice;
import com.shicijiaoliu.service.Userservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/manager")
public class managercontroller {

    @Autowired
    private Shiciservice shiciservice;

    @Autowired
    private Authorservice authorservice;

    @Autowired
    private Userservice userservice;

    @GetMapping("/shicilist")
    public result<Object> shicilist(int countpage, int pagenumber,
                                    @RequestParam(required = false) String title,
                                    @RequestParam(required = false) String dynasty,
                                    @RequestParam(required = false) String subject,
                                    @RequestParam(required = false) String author){

        List<Shicitabel> shicilist = shiciservice.findshiciBypage(countpage,pagenumber,title,dynasty,subject,author);
        if(shicilist == null){
            return result.error("获取页大于总页数");
        }else{
            int count = shiciservice.getcount(title,dynasty,subject,author);
            Map<String,Object> data = new HashMap<>();
            data.put("total",count);
            data.put("items",shicilist);
            return result.success(data);}
    }

    @PostMapping("/addshici")
    public result addshici(String addtitle,String addpara,String addauthor,String adddynasty,String addsubject){
        Shicitabel shici = new Shicitabel(addauthor,addtitle,addpara,addsubject,adddynasty);

        if(shiciservice.findByparagraphs(addpara) == null){
            if(authorservice.findByname(addauthor) == null){
                return result.error("作者不存在，请先添加作者");
            }
            shici.setAuthorId(authorservice.findByname(addauthor).getId());
            shiciservice.useraddshici(shici);
            return result.success();
        }else {
            return result.error("诗词内容已创作，请重新编写");
        }
    }

    @PutMapping("/updateshici")
    public result updatashici(String addtitle,String addpara,String addauthor,String adddynasty,String addsubject,long addId){
        Shicitabel shici = new Shicitabel(addauthor,addtitle,addpara,addsubject,adddynasty,addId);
        if(authorservice.findByname(addauthor) == null){
            Author author = new Author();
            author.setName(addauthor);
            author.setDynasty(adddynasty);
            authorservice.addauthor(author);
        }
        shici.setAuthorId(authorservice.findByname(addauthor).getId());
        shiciservice.updateshici(shici);
        return result.success();
    }

    @DeleteMapping("/delshici/{idS}")
    public result delshici(@PathVariable("idS")long idS){
        if(shiciservice.findByid(idS) == null){
            return result.error("没有找到该诗词");
        }else {
            shiciservice.delByid(idS);
            return result.success();
        }
    }

    @GetMapping("/authorlist")
    public result<Object> authorlist(int countpage, int pagenumber,
                                     @RequestParam(required = false) String name,
                                     @RequestParam(required = false) String dynasty){

        List<Author> authorlist = authorservice.findauthorBypage(countpage,pagenumber,name,dynasty);
        if(authorlist == null){
            return result.error("获取页大于总页数");
        }else{
            int count = authorservice.getcount(name,dynasty);
            Map<String,Object> data = new HashMap<>();
            data.put("total",count);
            data.put("items",authorlist);
            return result.success(data);}
    }

    @PostMapping("/addauthor")
    public result addauthor(String name,String bi,String dynasty){
        Author author = new Author(name,bi,dynasty);
        if(authorservice.findByname(name) == null){
            authorservice.addauthor(author);
            return result.success();
        }else {
            return result.error("作者已存在");
        }
    }

    @PutMapping("/updateauthor")
    public result updateauthor(String name,String bi,String dynasty,long id) {
        Author author = new Author(name, bi, dynasty, id);
        if (authorservice.findByid(id) == null) {
                return result.error("没有找到该作者");
        } else {
            authorservice.updateauthor(author);
            return result.success();
        }
    }

    @DeleteMapping("/delauthor/{id}")
    public result delauthor(@PathVariable("id")long id){
        if (authorservice.findByid(id) == null) {
            return result.error("没有找到该作者");
        } else {
            authorservice.delauthor(id);
            return result.success();
        }
    }

    @GetMapping("/userlist")
    public result<Object> userlist(int countpage, int pagenumber,
                                     @RequestParam(required = false) String username,
                                     @RequestParam(required = false) String nickname){

        List<User> userlist = userservice.finduserBypage(countpage,pagenumber,username,nickname);
        if(userlist == null){
            return result.error("获取页大于总页数");
        }else{
            int count = userservice.getcount(username,nickname);
            Map<String,Object> data = new HashMap<>();
            data.put("total",count);
            data.put("items",userlist);
            return result.success(data);}
    }

    @DeleteMapping("/deluser/{id}")
    public result deluser(@PathVariable("id")long id){
        if (userservice.findByid(id) == null) {
            return result.error("没有找到该用户");
        } else {
            userservice.deluser(id);
            return result.success();
        }
    }

    @GetMapping("/shici/findtitle")
    public  result findshicibytitle(String title){
        if(title == null){
            return result.error("请输入正确的名称");
        }else {
            return result.success(shiciservice.findBytitle(title));
        }
    }






}
